package com.IClass;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.IClass.App.IClass;
import com.IClass.Server.AppJsonDefine.GenJavaCode.*;

public class IClassApi {

	public static String SERVER_HOST = "http://ltfll.gicp.net:8085/";
	
	public String SendBlogCommentText(String blogid,String userid,String commenttext)
	{
		//取得取得默认的HttpClient实例
		DefaultHttpClient httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		String url = IClassApi.SERVER_HOST + AppHttpHelp.SendCommentText(userid, blogid, commenttext);
		
		HttpGet request = new HttpGet(url);
		
		try 
		{
				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					
					return strResult;
					
					
					
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} 
		return "";
	}
	
	public void SendBBSText(String userid,String srcPath,String bbstextjson,NetUploadPictureListen listen)
	{
		String uploadUrl = IClassApi.SERVER_HOST + AppHttpHelp.SendBBSText(userid, bbstextjson);
		NetUtil.UpLoadImage(uploadUrl, srcPath, listen);
	}
	
	public String GetServerText(String url)
	{
		//取得取得默认的HttpClient实例
		DefaultHttpClient httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		HttpGet request = new HttpGet(url);
		
		try 
		{
				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					return strResult;
					
					
					
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} 
		return "";
		
	}
	
	public String GetUploadlookpicture(String userid)
	{
		//取得取得默认的HttpClient实例
		DefaultHttpClient httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		String url  = SERVER_HOST + AppHttpHelp.GetUploadlookpicture(userid);
		HttpGet request = new HttpGet(url);
		
		try 
		{
				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					IClass.GetPictureResutlData(strResult);
					
					return strResult;
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} 
		return "";
	}
	public String Register(String username, String password, String email) {
		//取得取得默认的HttpClient实例
		DefaultHttpClient httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		String url  = SERVER_HOST + AppHttpHelp.Register(username, password, email);
		HttpGet request = new HttpGet(url);
		
		try 
		{
				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					
					return strResult;
					
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
		
	}
	public String Login(String username, String password) {
		//取得取得默认的HttpClient实例
		DefaultHttpClient httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		String url  = SERVER_HOST + AppHttpHelp.Login(username, password);
		HttpGet request = new HttpGet(url);
		
		try 
		{
				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					
					return strResult;
					
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} 
		return "";
		
	}
	public String GetHotBlogs() {
				
		
		
		//取得取得默认的HttpClient实例
		HttpClient  httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		HttpGet request = new HttpGet(SERVER_HOST + AppHttpHelp.GetUserLastBlog());
		String strResult = "";
		try 
		{

				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					
					IClass.GetHotBlogsData(strResult);
					
					
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} 
		
		return strResult;
	}
	
	public void GetBlogCommont(String blogid)
	{
		//取得取得默认的HttpClient实例
		DefaultHttpClient httpClient = new DefaultHttpClient();   
		//创建HttpGet实例
		HttpGet request = new HttpGet(SERVER_HOST + AppHttpHelp.Gethomecomment(blogid));
		
		try 
		{
				HttpResponse response = httpClient.execute(request);
			
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
					
					JSONArray res = new JSONArray(strResult);
					List<homeblogcomment> list = new ArrayList<homeblogcomment>();
					for(int i = 0; i < res.length();i++)
					{
						JSONObject jsonobj = res.getJSONObject(i);
						
						homeblogcomment b = new homeblogcomment();
						b.uid =  jsonobj.getString("uid");
						b.dateline = jsonobj.getString("dateline");
						b.author = jsonobj.getString("author");
						b.authorid = jsonobj.getString("authorid");
						b.cid = jsonobj.getString("cid");
						b.id = jsonobj.getString("id");
						b.idtype = jsonobj.getString("idtype");
						b.message = jsonobj.getString("message");
						b.avatarurl = jsonobj.getString("avatarurl");

						list.add(b);

						
					}
					IClass.getBlogcomments().put(blogid,list);
					
				}
				
		} catch (ClientProtocolException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
